/**
 * Covalent Server Header File
 * 
 * File:        prototype.h
 * Brief:       BRIEF
 * Author:      Chen Kangbing
 * Email:       ckblau@outlook.com
 * Modified:    2021/10/08 17:06:19
 *
 */


#ifndef PROTOTYPE_H
#define PROTOTYPE_H

#ifdef __cplusplus
extern "C" {
#endif 


#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <arpa/inet.h>


typedef struct {
    struct sockaddr_in socket_cliaddr;
    int socket_clifd;
} WorkerTaskTypeDef;

struct server_type_def;

typedef struct {
    WorkerTaskTypeDef task;
    pthread_t thread_tid;
    char *down_buffer;
    int down_buffer_size;
    char *up_buffer;
    int up_buffer_size;
    char *log_req_header;
    int log_status;
    int log_rsp_size;
    struct server_type_def *server;
} WorkerThreadTypeDef;

typedef struct server_type_def {
    FILE *log_fh;
    pthread_mutex_t log_mut;
    int socket_port;
    struct sockaddr_in socket_srvaddr;
    int socket_srvfd;
    int max_pool_size;
    WorkerThreadTypeDef **thread_pool;
    sem_t connection_sem;
    sem_t queue_sem;
    pthread_mutex_t queue_mut;
    int max_connection;
    WorkerTaskTypeDef *task_queue;
    int queue_head;
    int queue_tail;
} ServerTypeDef;

typedef enum {
    HTTP_NULL = 0,
    HTTP_HEAD = 1,
    HTTP_GET = 2,
    HTTP_POST = 3
} MethodTypeEnum;

typedef struct {
    MethodTypeEnum method;
    char *path;
    int path_len;
    char *real_path;
    int real_path_len;
    char *req_body;
    int req_body_size;
    int status;
    char *rsp_header;
    int rsp_header_size;
    char *rsp_body;
    int rsp_body_size;
} TransactionTypeDef;

typedef struct {
    char *dir;
    int dir_len;
    char *file;
    int file_len;
    char *ext;
    int ext_len;
} ReqPathTypeDef;


#ifdef __cplusplus
}
#endif 

#endif
